package com.supreme.uploadserver.controller;

import com.supreme.commonboot.exception.BizException;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.uploadserver.component.MinIoService;
import com.supreme.uploadserver.service.FileProcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "upload")
public class UploadController {

    @Value("${upload-server.uploadPath}")
    private String supremeUploadPath;

    @Value("${upload-server.minioBucket}")
    private String minioBucket;

    @Resource
    private FileProcService fileProcService;

    @Resource
    private MinIoService minIoService;

    @GetMapping(value = "test")
    public AjaxResp uploadTest() {
        return AjaxResp.instance();
    }

    @PostMapping(value = "single")
    public AjaxResp singleUpload(@RequestParam(value = "file") MultipartFile multipartFile, @RequestParam(value = "filename") String filename) throws IOException {
        if (multipartFile.isEmpty()) {
            throw new BizException("multipart file is empty");
        }

        String contentType = multipartFile.getContentType();
        long size = multipartFile.getSize();
        String originalFilename = multipartFile.getOriginalFilename();
        String fieldName = multipartFile.getName();

        assert originalFilename != null;
        Path newFilePath = fileProcService.storeUploadFile(supremeUploadPath, originalFilename, multipartFile::transferTo);

        return new AjaxResp()
                .append("contentType", contentType)
                .append("size", size)
                .append("fieldName", fieldName)
                .append("originalFilename", originalFilename)
                .append("newFileName", newFilePath.getFileName().toString());
    }

    @PostMapping(value = "multiple")
    public AjaxResp multipleUpload(@RequestParam(value = "files") MultipartFile[] multipartFiles) throws IOException {
        List<String> storeFileList = new ArrayList<>();
        for (MultipartFile multipartFile : multipartFiles) {
            String originalFilename = multipartFile.getOriginalFilename();

            assert originalFilename != null;
            Path newFilePath = fileProcService.storeUploadFile(supremeUploadPath, originalFilename, multipartFile::transferTo);
            storeFileList.add(newFilePath.getFileName().toString());
        }

        return new AjaxResp()
                .append("storeFileList", storeFileList);
    }

    @PostMapping(value = "toMinio")
    public AjaxResp toMinio(
            @RequestParam(value = "file") MultipartFile multipartFile) throws IOException {
        if (multipartFile.isEmpty()) {
            throw new BizException("multipart file is empty");
        }

        String contentType = multipartFile.getContentType();
        long size = multipartFile.getSize();
        String originalFilename = multipartFile.getOriginalFilename();
        String fieldName = multipartFile.getName();

        final InputStream inputStream = multipartFile.getInputStream();
        minIoService.upload(minioBucket, originalFilename, inputStream, size, contentType);

        return new AjaxResp()
                .append("contentType", contentType)
                .append("size", size)
                .append("fieldName", fieldName)
                .append("originalFilename", originalFilename);
    }
}
